package edu.friday.controller;

import edu.friday.Service.SysRoleService;
import edu.friday.Service.SysUserService;
import edu.friday.common.base.BaseController;
import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TableDataInfo;
import edu.friday.model.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;

@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Autowired
    SysUserService userService;

    //不同的表一般由不同的Service去执行
    @Autowired
    SysRoleService roleService;

//    @GetMapping("/list")
//    public List<SysUser> list() {
//        return userService.selectUserList();
    @GetMapping("/list")
    public TableDataInfo list(SysUserVO user, Pageable pageable) {
        int pageNum = pageable.getPageNumber()-1;
        pageNum = Math.max(pageNum,0);
        pageable = PageRequest.of(pageNum, pageable.getPageSize());
        return userService.selectUserList(user,pageable);
    }
    @PostMapping
    public RestResult add(@RequestBody SysUserVO user) {
        if(UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))){
            return RestResult.error("新增用户"+user.getUserName()+"失败，账号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return RestResult.error("新增用户"+user.getUserName()+"失败，手机号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))){
            return RestResult.error("新增用户"+user.getUserName()+"失败，邮箱已存在");
        }
        user.setCreateBy("system");
        boolean flag=userService.insertUser(user);
        return toAjax(flag ? 1 : 0);
    }

    @DeleteMapping("/{userIds}")
    public RestResult delete(@PathVariable Long[] userIds) {
        return toAjax(userService.deleteUserByIds(userIds));
    }

    @PutMapping
    public RestResult update(@Validated @RequestBody SysUserVO user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
            return RestResult.error("修改用户"+user.getUserName()+"失败，手机号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))){
            return RestResult.error("修改用户"+user.getUserName()+"失败，邮箱已存在");
        }

        user.setUpdateBy("system");
        boolean flag=userService.updateUser(user);
        return toAjax(flag ? 1 : 0);
    }

@GetMapping(value = {"/","{userId}"})
    public RestResult getInfo(@PathVariable(value = "userId", required = false) Long userId) {
        RestResult ajax = RestResult.success();
        ajax.put("roles",roleService.selectRoleList());
        if (userId != null) {
            ajax.put(RestResult.DATA_TAG,userService.selectUserById(userId));
            ajax.put("userIds",roleService.selectRoleListByUserId(userId));
        }
        return ajax;
    }


}
